<template>
	<view class="searchLayout">
		<view class="search">
			<uni-search-bar @confirm="onSearch" @cancel="onClear" @clear="onClear" focus placeholder="搜索"
				v-model="queryParams.keyword">
			</uni-search-bar>
		</view>
		<view>
			<view class="history">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="removeHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>

			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in recommendList" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>
		</view>


		<view v-if="classList.length === 0" class="noSearch">
			<uv-empty mode="search"></uv-empty>
		</view>


		<view>
			<view class="list">
				<template v-for="item in classList" :key="item._id">
					<navigator :url="`/pages/preview/preview?id=${item._id}`" class="item">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</navigator>
				</template>

			</view>
			<view v-if="noData || classList.length"><uni-load-more :status="noData?'noMore':'loading'"
					class="loading-view" /></view>
		</view>


	</view>
</template>

<script setup>
	import {
		useSearchStore
	} from '@/stores/search.js'
	import {
		getSearchResult
	} from '@/api/requestInterface.js'
	import {
		useWallListStore
	} from '@/stores/wallList.js'

	const wallList = useWallListStore()
	const searchStore = useSearchStore()

	//查询参数
	const queryParams = ref({
		pageNum: 1,
		pageSize: 12,
		keyword: ""
	})

	//搜索历史词
	const historySearch = ref([])

	//热门搜索词
	const recommendList = ref(["美女", "帅哥", "宠物", "卡通"])

	//没有更多
	const noData = ref(false)

	//搜索结果列表
	const classList = ref([])


	// 加载缓存数据
	const initData = () => {
		historySearch.value = searchStore.searchRecord
	}

	initData()

	// 获取搜索结果
	const handleSearch = async () => {

		const {
			errCode,
			data
		} = await getSearchResult(queryParams.value)
		if (errCode === 0) {
			classList.value = [...classList.value, ...data]
			if (queryParams.value.pageSize > data.length) {
				noData.value = true
			}
			wallList.updateData(classList.value)
		}
	}


	//点击搜索
	const onSearch = () => {
		searchStore.updateData(queryParams.value.keyword)
		enmotyData(queryParams.value.keyword)
		initData()
		handleSearch()
	}

	//点击清除按钮
	const onClear = () => {
		queryParams.value.keyword = ''
		enmotyData('')
	}

	// 取消清空
	const enmotyData = (value) => {
		classList.value = []
		noData.value = false
		queryParams.value = {
			pageNum: 1,
			pageSize: 12,
			keyword: value
		}
	}

	//点击标签进行搜索
	const clickTab = (value) => {
		enmotyData(value)
		queryParams.value.keyword = value
		onSearch()
	}


	//点击清空搜索记录
	const removeHistory = () => {
		uni.showModal({
			title: "是否清空历史搜索？",
			success: res => {
				if (res.confirm) {
					searchStore.resetData()
					initData()
				}
			}
		})
	}

	//触底加载更多
	onReachBottom(() => {
		queryParams.value.pageNum++
		handleSearch()
	})

	//关闭所有页面
	onUnload(() => {
		wallList.resetData()
	})
</script>

<style lang="scss" scoped>
	.searchLayout {
		.search {
			padding: 0 10rpx;
		}

		.topTitle {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 32rpx;
			color: #999;
		}

		.history {
			padding: 30rpx;
		}

		.recommend {
			padding: 30rpx;
		}

		.tabs {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			padding-top: 20rpx;

			.tab {
				background: #F4F4F4;
				font-size: 28rpx;
				color: #333;
				padding: 10rpx 28rpx;
				border-radius: 50rpx;
				margin-right: 20rpx;
				margin-top: 20rpx;
			}
		}

		.list {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 5rpx;
			padding: 20rpx 5rpx;

			.item {
				height: 440rpx;

				image {
					width: 100%;
					height: 100%;
					display: block;
				}
			}
		}
	}
</style>